#include "iic.h"

/*
 * Function:        User_I2C_Init
 * Description:     Configure I2C
 * Param:           None.
 * Return:          None.
 */
void User_I2C_Init()
{
    IIC1_CLK_ENABLE();
    GPIOF_CLK_ENABLE();
    
    GPIO_InitTypeDef  GPIO_InitStructure;
    I2C_InitTypeDef I2C_InitStructure;
    
    /* Set GPIO multiplex mapping */
	GPIO_PinAFConfig(GPIOF, GPIO_PinSource13, GPIO_AF_IIC1);
	GPIO_PinAFConfig(GPIOF, GPIO_PinSource15, GPIO_AF_IIC1);

	/* GPIO Configure */
	GPIO_StructInit(&GPIO_InitStructure);
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
	GPIO_InitStructure.GPIO_OType = GPIO_Out_OD;
	GPIO_InitStructure.GPIO_OStrength = GPIO_OStrength_9mA;
	GPIO_Init(GPIOF, &GPIO_InitStructure);


	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
	GPIO_InitStructure.GPIO_OType = GPIO_Out_OD;
	GPIO_InitStructure.GPIO_OStrength = GPIO_OStrength_9mA;
	GPIO_Init(GPIOF, &GPIO_InitStructure);

    IIC1_RESET();
    delay_ms(100);
    IIC1_SET();
	/* Initializes the I2Cx */
	I2C_StructInit(&I2C_InitStructure);
	I2C_InitStructure.I2C_ClockScale = I2C_PCLK_960;
	I2C_InitStructure.I2C_EN = ENABLE;
	I2C_InitStructure.I2C_SlaveAddress = 0x01;
	I2C_InitStructure.I2C_Ack = I2C_IICAA_ACK;
	I2C_InitStructure.I2C_ICLKDIVH = 0x02;
	I2C_InitStructure.I2C_ICLKDIVL = 0x20;
	I2C_Init(I2C1, &I2C_InitStructure);

}
